package com.klhappy.houyuan.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.ExtractMoneyBean.AccBean;
import com.klhappy.houyuan.bean.RegisterBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.ConfirmMoneyDialog;
import com.klhappy.houyuan.dialog.NetWorkDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.GsonRequest;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/11/21.
 */
public class ConfirmMoneyActivity extends AbsActivity implements CallBackInterModle{
    public static final String AVD = "ConfirmMoneyActivity";
    public static final String AVG = "ConfirmMoney";
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.btn_comfirm)
    Button sendCodeBtn;
    @InjectView(R.id.et_verification)
    EditText et_verification;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }
    @OnClick(R.id.btn_comfirm)
    public void btn_comfirm() {
        mConfigManage.remove(KeyConstant.cookie);
        if (count == 60) {
            sendCodeBtn.setClickable(false);
            sendCodeBtn.setBackgroundResource(R.drawable.corner_bg3);
            sendCodeBtn.setTextColor(0xffffffff);
            {
                refreshReg = false;
                regist(mConfigManage.get(KeyConstant.account ,""));
            }
        }
    }
    @OnClick(R.id.tv_confirm)
    public void tv_confirm(){
        verification = et_verification.getText().toString();
        if (!TextUtils.isEmpty(verification)){
            dialog = new ConfirmMoneyDialog();
            dialog.setDialog(this , money, mConfigManage.get(KeyConstant.Account, ""));
            dialog.Show();
        }else {
            showToastx("请输入验证码");
        }
    }

    private boolean refreshReg = false;
    private int count = 60;
    private Handler handlerBtn = new Handler();
    private JiaYuanModle modle;
    private ConfirmMoneyDialog dialog;
    private String money;
    private String verification;
    private AccBean accBean;

    private String mCookie;
    private GsonRequest<RegisterBean> mRequest = null;
    private NetWorkDialog Ndialog;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.confirm_money);
        ButterKnife.inject(this);
        title.setText("提现申请");

        modle = new JiaYuanModle(this);
        modle.setCallBackInterModle(this);
        Ndialog = new NetWorkDialog(this);

        money = (String) getIntent().getSerializableExtra("moneyx");

        NotifyMethod.getInstance().addObserver(AVD, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                accBean = new AccBean();
                accBean.setAccBean(mConfigManage.get(KeyConstant.accname, "")
                        , mConfigManage.get(KeyConstant.Account, "")
                        , "1", money
                        , (String) o
                        , verification);

                modle.setTixian(accBean);
            }
        });
        NotifyMethod.getInstance().addObserver(AVG, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                finish();
            }
        });
    }

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (count > 0) {
                if (refreshReg) {
                    refresh();
                    return;
                }
                count--;
                sendCodeBtn.setText(count + "秒后重发");
                handlerBtn.postDelayed(runnable, 1000);
            } else if (count == 0) {
                refresh();
                return;
            }
        }
    };

    private void refresh() {
        count = 60;
        sendCodeBtn.setText("发送验证码");
        sendCodeBtn.setClickable(true);
        sendCodeBtn.setTextColor(Color.WHITE);
        sendCodeBtn.setBackgroundResource(R.drawable.corner_bg2);
        handlerBtn.removeCallbacks(runnable);
        return;
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.SETACCOUNT_URL)){
            if (o.getCode() == 200){
                Ndialog.netWork(o.getMsg());
                Ndialog.show();
                dialog.Dismiss();
            }else {
                showToastx(o.getMsg());
            }
        }
    }

    private void regist(String account) {
        mConfigManage.remove(KeyConstant.cookie);
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", account);
        Response.Listener<RegisterBean> listener = new Response.Listener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean response) {
                Gson gson = new Gson();
                Log.e("pu", gson.toJson(response));
                if (response.code == 200){
                    showToastx("验证码发送成功,请稍等...");
                    handler.post(runnable);
                    mConfigManage.set(KeyConstant.cookie ,mCookie);
                }else {
                    showToastx(response.msg);
                }
            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                showToast("网路错误");
            }
        };
        mRequest = new GsonRequest<RegisterBean>(Request.Method.POST, UrlConstant.GET_REGIST_URL, map,
                RegisterBean.class, listener, errorListener) {
            @Override
            protected Response<RegisterBean> parseNetworkResponse(NetworkResponse response) {
                mCookie = getCookie(response);
                return super.parseNetworkResponse(response);
            }
        };
        try {
            mRequest.setCookie(mConfigManage.get(KeyConstant.session ,""));
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        getAppEnv().getVolleyRequest().add(mRequest);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NotifyMethod.getInstance().removeObserver(AVD);
        NotifyMethod.getInstance().removeObserver(AVG);
    }
}
